import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisSet {
    public static void test1(Jedis jedis) {
        System.out.println("sadd  smembers  sismember");
        jedis.flushAll();

        jedis.sadd("key1", "111","222","333","444","555","666");

        // 获取 key 这个关键字对应的所有元素集合
        Set<String> key1 = jedis.smembers("key1");
        System.out.println(key1);

        // 在key 这个关键字对应的 value中查询是否存在这个 memeber元素
        boolean key11 = jedis.sismember("key1", "555");
        System.out.println(key11);
    }

    public static void test2(Jedis jedis) {
        System.out.println("spop   srandmember");
        jedis.flushAll();

        jedis.sadd("key1", "111","222","333","444","555","666");

        // 随机删除 key 中的一个元素
        String key1 = jedis.spop("key1");
        System.out.println(key1);

        // 随机获取一个 key 中的元素，但是不会删除对应的元素
        String key11 = jedis.srandmember("key1");
        System.out.println(key11);
    }

    public static void test3(Jedis jedis) {
        System.out.println("smove  srem");
        jedis.flushAll();

        jedis.sadd("key1", "111","222","333","444","555","666");

        // 将member从 srckey 中删除，并将其移动到 dstkey 中
        jedis.smove("key1", "key2", "222");
        Set<String> key1 = jedis.smembers("key1");
        System.out.println(key1);
        Set<String> key2 = jedis.smembers("key2");
        System.out.println(key2);

        // 从key 中将 member删除，可以删除多个
        jedis.srem("key1", "111","333","444");
        System.out.println(key1);
    }

    public static void test4(Jedis jedis) {
        System.out.println("sinter  sinterstore");
        jedis.flushAll();

        jedis.sadd("key1", "111","222","333","444","555","666");
        jedis.sadd("key2", "555","666","777","888","999");

        // 对这些 key 进行交集操作
        Set<String> sinter = jedis.sinter("key1", "key2");
        System.out.println(sinter);

        // 对这些key进行交集操作，之后将这个交集的结果放入到dstkey 中
        jedis.sinterstore("key3", "key1","key2");
        System.out.println(jedis.smembers("key3"));
    }

    public static void test5(Jedis jedis) {
        System.out.println("sunion  sunionstore");
        jedis.flushAll();

        jedis.sadd("key1", "111","222","333","444","555","666");
        jedis.sadd("key2", "555","666","777","888","999");

        // 对这些 key 进行并集操作
        Set<String> sunion = jedis.sunion("key1", "key2");
        System.out.println(sunion);

        // 对这些key进行并集操作，之后将这个并集的结果放入到dstkey 中
        jedis.sunionstore("key3", "key1","key2");
        System.out.println(jedis.smembers("key3"));
    }

    public static void test6(Jedis jedis) {
        System.out.println("sdiff  sdiffstore");
        jedis.flushAll();

        jedis.sadd("key1", "111","222","333","444","555","666");
        jedis.sadd("key2", "555","666","777","888","999");

        // 对这些 key 进行差集操作
        Set<String> sdiff = jedis.sdiff("key1", "key2");
        System.out.println(sdiff);

        // 对这些key进行差集操作，之后将这个差集的结果放入到dstkey 中
        jedis.sdiffstore("key3", "key1","key2");
        System.out.println(jedis.smembers("key3"));
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis =  jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
//            test6(jedis);
        }
    }
}
